regular expressionNN 4   IE J3   ECMA n/a

A regular expression object is an instance of the RegExp object. Each regular expression object consists of a pattern that is used to locate matches within a string. Patterns for a regular expression can be simple strings or significantly more powerful expressions that use a notation that is essentially a language unto itself. The implementation of regular expressions in JavaScript 1.2 is very similar to the way they are implemented in Perl. You can read more about these concepts in books covering JavaScript 1.2.

To create a regular expression object, surround the pattern with forward slashes, and assign the whole expression to a variable. For example, the following statement creates a regular expression whose pattern is a simple word:

var re = /greet/

The re variable can then be used as a parameter in a variety of methods that search for the pattern within some string (you may also use an expression directly as a method parameter, rather than assigning it to a variable).

Regular expression notation also consists of a number of metacharacters that stand in for sometimes complex ideas, such as the boundary on either side of a word, any numeral, or one or more characters. For example, to search for the pattern of characters shown above but only when the pattern is a word (and not part of a word such as greetings), the regular expression notation uses the metacharacters to indicate that the pattern includes word boundaries on both sides of the pattern:

var re = /\bgreet\b/

shows a summary of the regular expression notation used in JavaScript 1.2:

Regular Expression Notation

Character

Matches

Example

\b

Word boundary

/\bto/ matches "tomorrow"

/to\b/ matches "Soweto"

/\bto\b/ matches "to"

\B

Word nonboundary

/\Bto/ matches "stool" and "Soweto"

/to\B/ matches "stool" and "tomorrow"

/\Bto\B/ matches "stool"

\d

Numeral 0 through 9

/\d\d/ matches "42"

\D

Nonnumeral

/\D\D/ matches "to"

\s

Single whitespace

/under\sdog/ matches "under dog"

\S

Single nonwhitespace

/under\Sdog/ matches "under-dog"

\w

Letter, numeral, or underscore

/1\w/ matches "1A"

\W

Not a letter, numeral, or underscore

/1\W/ matches "1%"

.

Any character except a newline

/../ matches "Z3"

[...]

Any one of the character set in brackets

/J[aeiou]y/ matches "Joy"

[^...]

Negated character set

/J[^eiou]y/ matches "Jay"

*

Zero or more times

/\d*/ matches "", "5", or "444"

?

Zero or one time

/\d?/ matches "" or "5"

+

One or more times

/\d+/ matches "5" or "444"

{n}

Exactly n times

/\d{2}/ matches "55"

{n,}

n or more times

/\d{2,}/ matches "555"

{n,m}

At least n, at most m times

/\d{2,4}/ matches "5555"

^

At beginning of a string or line

/^Sally/ matches "Sally says..."

$

At end of a string or line

/Sally.$/ matches "Hi, Sally."

When you create a regular expression, you may optionally wire the expression to work globally (as you probably do if the regular expression is doing a search-and-replace operation with a method) and to ignore case in its matches. The modifiers that turn on these switches are the letters g and i. They may be used by themselves or together as gi.

Once you have established a pattern with the regular expression notation, all the action takes place in the regular expression object methods and the String object methods that accept regular expression parameters.

 
Creating a regular expression Object
var regExpressionObj = /pattern/ [g | i | gi]
var regExpressionObj = new RegExp(["pattern", ["g" | "i" | "gi"]])
global, ignoreCaseNN 4   IE J3   ECMA n/a
 Read-only
 

Returns whether the object had the g or i modifiers set when it was created. If a regular expression object has both modifiers set (gi), you must still test for each property individually.

 
Example
if (myRE.global && myRE.ignoreCase) {
    ...
}
 
Value
Boolean.
lastIndexNN 4   IE J3   ECMA n/a
 Read/Write
 

The zero-based index value of the character within the string where the next search for the pattern begins. In a new search, the value is zero. You can also set the value manually if you wish to start at a different location or skip some characters.

 
Example
myRE.lastIndex = 30
 
Value
Integer.
sourceNN 4   IE J3   ECMA n/a
 Read-only
 

Returns a string version of the characters used to create the regular expression. The value does not include the forward slash delimiters that surround the expression.

 
Example
var myREasString = myRE.source
 
Value
String.
compile()NN 4   IE J3   ECMA n/a
compile(pattern[, g | i | gi]) 
 

Compiles a regular expression pattern into a genuine regular expression object. This method is used primarily to recompile a regular expression whose pattern may change during the execution of a script.

 
Returned Value
regular expression object reference.
 
Parameters
pattern Any regular expression pattern as a quoted string.
exec()NN 4   IE J3   ECMA n/a
exec(string) 
 

Performs a search through the string passed as a parameter for the current regular expression pattern. A typical sequence follows the format:

var myRE = /somePattern/
var resultArray = myRE.exec("someString")

Properties of both the static RegExp and regular expression (myRE in the example) objects are updated with information about the results of the search. In addition, the exec() method returns an array of data, much of it similar to RegExp object properties. The returned array includes the following properties:

index

Zero-based index of starting character in the string that matches the pattern

input

The original string being searched

[0]

String of the characters matching the pattern

[1]...[n]

Strings of the results of the parenthesized component matches

You can stow away the results of the exec() method in a variable, whereas the RegExp property values change with the next regular expression operation. If the regular expression is set for global searching, a subsequent call to myRE.exec("someString") continues the search from the position of the previous match.

If no match is found for a given call to exec(), it returns null.

 
Returned Value
An array of match information if successful; null if there is no match.
 
Parameters
string The string to be searched.
test()NN 4   IE J3   ECMA n/a
test(string) 
 

Returns true if there is a match of the regular expression anywhere in the string passed as a parameter; false if not. No additional information is available about the results of the search. This is the fastest way to find out if a pattern matches within a string.

 
Returned Value
Boolean.
 
Parameters
string The string to be searched.